package soul.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Created by yangyincong on 15/8/16.
 * 手动读取properties配置文件
 */
public class PropertiesReader {

    private final Logger log = LoggerFactory.getLogger(PropertiesReader.class);

    private Properties props = new Properties();

    public Properties getProperties(){
        return this.props;
    }

    public PropertiesReader(String path){
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(PropertiesReader.class.getClassLoader().getResourceAsStream(
                    path)));
            props.load(bf);
            if(log.isDebugEnabled()){
                log.debug("Properties result============= start");
                for (Object o : props.keySet()) {
                    log.debug(o+"=>"+props.get(o));
                }
                log.debug("Properties result============= end");
            }
        } catch (UnsupportedEncodingException e) {
            log.error("load properties error" + e.getMessage());
        } catch (IOException e) {
            log.error("load properties error" + e.getMessage());
        }
    }

    public String getProperty(String key,String defaultValue){
        try {
            if (null != props) {
                return props.getProperty(key, defaultValue);
            }
        }catch (Exception ex){}
        return defaultValue;
    }

    public String getProperty(String key){
        if(null!=props){
            try {
                return props.getProperty(key);
            }catch (Exception ex){}
        }
        return null;
    }
}

